import logging
import requests

from conf.config import KUAIDI100_CUSTOMER
from helper.payment.wechat_pay import md5

logger = logging.getLogger("common")


class ExpressHelper(object):
    @staticmethod
    def sign(param):
        customer = KUAIDI100_CUSTOMER
        key = "JEKLlGds3617"
        content = "%s%s%s" % (param, key, customer)
        ret = md5(content.encode()).upper()
        return ret

    @staticmethod
    def query(symbol, no):
        """
        查询物流信息
    接口数据示例：
        {
  "message": "ok",
  "nu": "220032893603",
  "ischeck": "1",
  "condition": "F00",
  "com": "annengwuliu",
  "status": "200",
  "state": "3",
  "data": [
    {
      "time": "2017-10-13 14:16:30",
      "ftime": "2017-10-13 14:16:30",
      "context": "【2017-10-13 14:16:30】快件已被客户【郭先生签收,签收网点【罗湖口岸】"
    },
    {
      "time": "2017-10-13 11:04:06",
      "ftime": "2017-10-13 11:04:06",
      "context": "【2017-10-13 11:04:06】快件已到达【罗湖口岸】"
    },
    {
      "time": "2017-10-13 09:10:12",
      "ftime": "2017-10-13 09:10:12",
      "context": "【2017-10-13 09:10:12】快件在【罗湖】正发往【罗湖口岸】"
    },
    {
      "time": "2017-10-13 08:32:30",
      "ftime": "2017-10-13 08:32:30",
      "context": "【2017-10-13 08:32:30】快件已到达【罗湖】"
    },
    {
      "time": "2017-10-13 04:42:06",
      "ftime": "2017-10-13 04:42:06",
      "context": "【2017-10-13 04:42:06】快件在【盐田分拨中心】正发往【罗湖】"
    },
    {
      "time": "2017-10-12 22:11:52",
      "ftime": "2017-10-12 22:11:52",
      "context": "【2017-10-12 22:11:52】快件已到达【盐田分拨中心】"
    },
    {
      "time": "2017-10-12 13:37:39",
      "ftime": "2017-10-12 13:37:39",
      "context": "【2017-10-12 13:37:39】快件在【广州分拨中心】正发往【盐田分拨中心】"
    },
    {
      "time": "2017-10-12 07:01:46",
      "ftime": "2017-10-12 07:01:46",
      "context": "【2017-10-12 07:01:46】快件已到达【广州分拨中心】"
    },
    {
      "time": "2017-10-12 01:33:34",
      "ftime": "2017-10-12 01:33:34",
      "context": "【2017-10-12 01:33:34】快件在【北滘分拨中心】正发往【广州分拨中心】"
    },
    {
      "time": "2017-10-11 23:26:05",
      "ftime": "2017-10-11 23:26:05",
      "context": "【2017-10-11 23:26:05】快件已到达【北滘分拨中心】"
    },
    {
      "time": "2017-10-11 22:16:07",
      "ftime": "2017-10-11 22:16:07",
      "context": "【2017-10-11 22:16:07】快件在【容桂华天路】正发往【北滘分拨中心】"
    },
    {
      "time": "2017-10-11 19:27:28",
      "ftime": "2017-10-11 19:27:28",
      "context": "【2017-10-11 19:27:28】快件已由【容桂华天路】揽收,收件员【容桂华天路】"
    }
  ]
}
        :param symbol:
        :param no:
        :return:
        """
        url = "https://poll.kuaidi100.com/poll/query.do"
        param = '{"com":"' + symbol.lower() + '","num":"' + no + '"}'
        s = ExpressHelper.sign(param)
        data = dict(
            customer=KUAIDI100_CUSTOMER,
            sign=s,
            param=param
        )
        logger.debug("request=%s" % data)
        r = requests.post(url=url, data=data, verify=False)
        logger.debug(r.text)
        obj = r.json()
        if obj.get("message") != "ok":
            return None
        return obj.get("data")


if __name__ == "__main__":
    a = ExpressHelper.query("annengwuliu", "220032893603")
    print(a)
